const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');
Page({
	data: {
		sex_list: [{
				id: 1,
				sex_name: '男'
			},
			{
				id: 2,
				sex_name: '女'
			}
		],
		school_list: [{
				id: 1,
				name: '隆安中心小学'
			},
			{
				id: 2,
				name: '周宁中心小学'
			}
		],
		parentage_list: [{
				id: 0,
				parentage_name: '爸爸'
			},
			{
				id: 1,
				parentage_name: '妈妈'
			}
		], //亲子关系
		grade_list: [{
				id: 3,
				grade_name: '一年级'
			},
			{
				id: 4,
				grade_name: '二年级'
			},
			{
				id: 5,
				grade_name: '三年级'
			}
		],
		class_list: [{
				id: 6,
				class_name: '一班'
			},
			{
				id: 7,
				class_name: '二班'
			}
		],
		add_item: {
			parentage_index: '',
			student_name: '',
			sex_index: '',
			birthday: '',
			school_index: '',
			grade_index: '',
			class_index: ''
		},
		edit_item: {},
		schoolIdDauflt: 0,
		initFlag: false, //初始化参数
		isShow: false, //模态框参数
		sname: 'xxx' ,//模态框参数-选中学生姓名
		no_addstudent:false,//防止多次点击新增学生
	},
	navback() {
		wx.navigateBack({
			delta: 1,
		})
	},
	onReady: function(options) {

	},
	onLoad(options) {
		this.getSchoolList(); //选项框-获取学校-班级
		this.getGradeList(); //选项框-获取年级
		this.getStudentList(); //学生列表初始化
		this.getParentageList();
	},
	getStudentList() {
		var that = this;
		http.httpPost(config.get_student_list, {}, function(e) {
			that.student_list = e.data;
			if(that.data.initFlag==false && e.data.length>0){
				that.data.initFlag = true;
				that.student_list_active_id = e.data[0].id;	//默认选择第一个孩子
				that.student_active_index = 0;
			
				that.setData({
					'student_list': e.data,
					'initFlag': true,
					'student_list_active_id': e.data[0].id,
					'student_active_index': 0,
					'mode': 'edit',
					'edit_item': e.data[0]
				})
			}
			
			
		});
	},
	//更改亲子关系
	parentageChange: function(e) {
		let add_item = this.data.add_item;
		add_item.parentage_index = e.detail.value;
		this.setData({
			add_item: add_item
		})
	},
	//更改学生姓名
	studentnameChange: function(e) {
		let add_item = this.data.add_item;
		add_item.student_name = e.detail.value;
		this.setData({
			add_item: add_item
		})
	},
	//更改学生性别
	sexChange: function(e) {
		let add_item = this.data.add_item;
		add_item.sex_index = e.detail.value;
		this.setData({
			add_item: add_item
		})
	},
	//更改出生日期
	birthdayChange: function(e) {
		let add_item = this.data.add_item;
		add_item.birthday = e.detail.value;
		this.setData({
			add_item: add_item
		})
	},
	//更改学校
	schoolChange: function(e) {
		let add_item = this.data.add_item;
		add_item.school_index = e.detail.value;
		this.setData({
			add_item: add_item
		})

		//改变班级列表
		this.schoolIdDauflt = this.school_list[e.detail.value].id;
		this.getClassList(this.schoolIdDauflt);
		
		//清空年级、班级
		add_item.grade_index = '';
		add_item.class_index = '';
		this.setData({
			add_item: add_item
		});
	},
	//更改年级
	gradeChange: function(e) {
		let add_item = this.data.add_item;
		add_item.grade_index = e.detail.value;
		this.setData({
			add_item: add_item
		});
		
		let grade_id = this.data.grade_list[e.detail.value].id;
		this.getClassList(this.schoolIdDauflt,grade_id);
		
		//清空班级选择
		add_item.class_index = '';
		this.setData({
			add_item: add_item
		});
	},
	getParentageList(){//获取亲子关系
		var that = this;
		http.httpPost(config.get_parentage_list, {}, function(e) {
			that.parentage_list = e.data;
			
			that.setData({
				'parentage_list': e.data
			})
			
		});
	},
	getSchoolList() { //获取【校园列表】
		var that = this;
		http.httpPost(config.get_school_list, {
			showStatus: true
		}, function(e) {
			that.school_list = e.data;
			that.setData({
				'school_list': e.data
			})
			//
			if (e.data.length > 0) {
				that.schoolIdDauflt = e.data[0].id;
				that.setData({
					schoolIdDauflt: e.data[0].id
				})
				var school_id = e.data[0].id;
				that.getClassList(school_id)
			}
		
		});

	},
	getClassList(school_id =null,grade_id = null) { //获取【报名课程】
		var that = this;
		http.httpPost(config.get_studentclasses_list, {
			school_id: school_id,
			grade_id:grade_id
		}, function(e) {
			that.class_list = e.data;

			that.setData({
				'class_list': e.data
			})
		});
	},
	getGradeList() { //获取【年级】
		var that = this;
		http.httpPost(config.get_grade_list, {
			showStatus: true
		}, function(e) {
			that.grade_list = e.data;
		
			that.setData({
				'grade_list': e.data
			})
		
		});
	},
	//更改班级
	classChange: function(e) {
		let add_item = this.data.add_item;
		add_item.class_index = e.detail.value;
		this.setData({
			add_item: add_item
		})

	},
	//添加学生
	addStudent: function(e) {
		if(!this.data.no_addstudent){
			this.data.no_addstudent = true;
			this.setData({
				no_addstudent: true
			})
		}else{
			wx.showToast({
				title: "添加学生中，请稍后再试",
				icon: 'none',
				duration: 1000,
				mask: true
			});
		}
		
		let add_item = {};
		if (this.data.add_item.parentage_index == '') {
			wx.showToast({
				title: '请选择亲子关系',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.parentage_name = this.data.parentage_list[this.data.add_item.parentage_index].parentage_name;
		
		if (this.data.add_item.student_name == '') {
			wx.showToast({
				title: '请输入学生姓名',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.student_name = this.data.add_item.student_name;
	
		if (this.data.add_item.sex_index == '') {
			wx.showToast({
				title: '请选择学生性别',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.sex = this.data.sex_list[this.data.add_item.sex_index].id;
	
		if (this.data.add_item.birthday == '') {
			wx.showToast({
				title: '请选择出生日期',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.birthday = this.data.add_item.birthday;
	
		if (this.data.add_item.school_index == '') {
			wx.showToast({
				title: '请选择学校',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.school_id = this.data.school_list[this.data.add_item.school_index].id;
		add_item.school_name = this.data.school_list[this.data.add_item.school_index].name;
	
		if (this.data.add_item.grade_index == '') {
			wx.showToast({
				title: '请选择年级',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.grade_id = this.data.grade_list[this.data.add_item.grade_index].id;
		add_item.grade_name = this.data.grade_list[this.data.add_item.grade_index].grade_name;
	
		if (this.data.add_item.class_index == '') {
			wx.showToast({
				title: '请选择班级',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		add_item.class_id = this.data.class_list[this.data.add_item.class_index].id;
		add_item.class_name = this.data.class_list[this.data.add_item.class_index].class_name;
	
		let student_list = this.data.student_list;
		// student_list.push(add_item);
	
		//添加孩子信息
		var that = this;
		http.httpPost(config.get_add_childdata, {
			parentage:that.data.add_item.parentage_index,
			studentname: that.data.add_item.student_name,
			school_id: that.data.school_list[that.data.add_item.school_index].id,
			sex: that.data.sex_list[that.data.add_item.sex_index].id,
			birthtime: that.data.add_item.birthday,
			grade_id: that.data.grade_list[that.data.add_item.grade_index].id,
			class_id: that.data.class_list[that.data.add_item.class_index].id,
		}, function(e) {
			wx.showToast({
				title: e.msg,
				icon: 'none',
				duration: 1000,
				mask: true
			});
			if (e.code == 1) {
				add_item.id = e.data;
				student_list.push(add_item);
			}
			that.setData({
				'student_list': student_list,
				'add_item': {
					parentage_index:'',
					student_name: '',
					sex_index: '',
					birthday: '',
					school_index: '',
					grade_index: '',
					class_index: ''
				},
				"no_addstudent" : false
			});
			setTimeout(function(){
				wx.redirectTo({
					url: './personal'
				})
			},2000);
		});
	
	},


})
